/**
 * 
 */
package martinhynar.blog;

import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.WebServiceContext;

import martinhynar.blog.limitedservice.v1_0.LimitedService;

import org.jboss.wsf.spi.annotation.WebContext;

@WebContext(contextRoot = "/webservice", urlPattern = "/LimitedService", secureWSDLAccess = false)
@PermitAll
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@WebService(name = "LimitedService", targetNamespace = "urn:martinhynar:blog:limitedservice:v1_0", endpointInterface = "martinhynar.blog.limitedservice.v1_0.LimitedService")
@HandlerChain(file = "resource://META-INF/jaxws-handlers.xml")
@Stateless
public class LimitedServiceSession implements LimitedService {

	/** Web service context. */
	@Resource
	WebServiceContext wsCtx;

	private static final Logger LOG = Logger
			.getLogger(LimitedServiceSession.class.getName());

	public String limitedServiceRequest(String parameter) {
		Boolean limit = (Boolean) wsCtx.getMessageContext().get(
				ThroughputLimitHandler.LIMIT_REACHED);
		Object requestId = wsCtx.getMessageContext().get(
				ThroughputLimitHandler.REQUEST_ID);

		if (limit) {
			LOG.info(String.format(
					"[R: %s] Throughput limit reached, rejecting message.",
					requestId));
			return null;
		}

		String upperCase = parameter.toUpperCase();
		LOG.info(String.format(
				"[S: %s] Received \"%s\", returning converted to upper case \"%s\".",
				requestId, parameter, upperCase));
		return upperCase;
	}

}